{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 4.6 随机数的生成 Random Number Generation\r\n",
    "\r\n",
    "`numpy.random` 模块：\r\n",
    "\r\n",
    "+ `numpy.random.seed()`：随机数生成器种子\r\n",
    "+ `numpy.random.permutation()`：返回一个序列的随机排列或返回一个随机排列的范围；\r\n",
    "+ `numpy.random.shuffle()`：对一个序列就地随机排列；\r\n",
    "+ `numpy.random.randint(low=, high=, size=())`：随机整数（Random integers），不加关键词输入参数默认为最大值，可以省略最低值，size可以指定数组的shape（高维度需要用元组的形式）；\r\n",
    "+ `numpy.random.randn()`：产生样本值：标准正态分布（均值为 0，标准差为 1）\r\n",
    "+ `numpy.random.rand()`：产生样本值：均匀分布（Uniform distribution）\r\n",
    "+ `numpy.random.uniform()`：产生样本值： [0,1) 中的均匀分布；\r\n",
    "+ `numpy.random.binomial()`：产生样本值：二项分布（Binomial distribution）；\r\n",
    "+ `numpy.random.poisson()`：产生样本值： 泊松分布（Poisson distribution）；\r\n",
    "+ `numpy.random.normal(loc=, scale=, size=())`：产生样本值：正态分布（Normal distribution），或称为高斯分布（Guess distribution）；\r\n",
    "+ `numpy.random.standard_normal()`：产生样本值：标准正态分布（Standard normal distribution）；\r\n",
    "+ `numpy.random.exponential()`：产生样本值： 指数分布（Exponential distribution）；\r\n",
    "+ `numpy.random.beta()`：产生样本值：Beta 分布（Beta distribution）；\r\n",
    "+ `numpy.random.gamma()`：产生样本值：Gamma 分布（Gamma distribution）；\r\n",
    "+ `numpy.random.chisquare()`：产生样本值：卡方分布（Chi-square distribution）；\r\n",
    "+ `numpy.random.standard_t()`：产生样本值：标准 t 分布（Student's t-distribution）；\r\n",
    "+ `numpy.random.f()`：产生样本值：F 分布（Fisher-Snedecor distribution, F distribution）；\r\n",
    "\r\n",
    "更多函数可以查阅 `numpy.random` 的说明（鼠标悬停在 “random”）"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 模块导入\r\n",
    "import os, sys\r\n",
    "sys.path.append(os.path.dirname(os.getcwd()))\r\n",
    "import random\r\n",
    "import numpy\r\n",
    "from dependency import arr_info"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 生成符合一定分布的随机数据\r\n",
    "\r\n",
    "samples_1 = numpy.random.normal(size=(10))      # 一维\r\n",
    "samples_2 = numpy.random.normal(size=(4,4))     # 二维\r\n",
    "\r\n",
    "arr_info([samples_1, samples_2])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 对比大数据量生成的速度\r\n",
    "\r\n",
    "N = 1000000\r\n",
    "\r\n",
    "%timeit samples = [random.normalvariate(0, 1) for x in list(range(N))]\r\n",
    "# 书中的 xrange() 函数在Python3中已经取消，可以用 list(range()) 实现相同的功能。\r\n",
    "# xrange()返回的是一个生成器，range()返回的是一个列表，\r\n",
    "# xrange对象是按需生成单个元素，而range()首先创建一个列表。\r\n",
    "# 一般来说，xrange()占用的空间会更小，也会更快。\r\n",
    "\r\n",
    "%timeit samples = numpy.random.normal(size=N)"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.6 64-bit ('DataAnalysis': venv)"
  },
  "interpreter": {
   "hash": "0fe90ac710750590bd916650f441b3b2233faa2658175d6302b22e90f225e38d"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}